package com.syt.service.controller;

import cn.hutool.db.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syt.service.pojo.PhoneBase;
import com.syt.service.service.PhoneBaseService;
import com.syt.service.util.LoginRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;

/**
 * 基础phone数据 控制器
 */
@RestController
public class PhoneBaseController {

    @Autowired
    private PhoneBaseService phoneBaseService;

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @RequestMapping("basePhone/get/{id}")
    public PhoneBase getByid(@PathVariable("id") Integer id) {
        return phoneBaseService.getPhoneByid(id);
    }

    /**
     * 分页获取 列表
     * @param id
     * @return
     */
    @RequestMapping("basePhone/list/{id}")
    public PageInfo<PhoneBase> listPhoneBase(@PathVariable("id") Integer id) {
        PageHelper.startPage(id, 10);
        PageInfo<PhoneBase> phoneBasePageInfo = new PageInfo<>(phoneBaseService.allListPhone());
        return phoneBasePageInfo;
    }

    /**
     * 分类查询
     * @param name
     * @return
     */
    @GetMapping("basePhone/classify")
    public List<PhoneBase> classifyByName(@RequestParam("name") String name) {
        return phoneBaseService.classifyPhone(name);
    }

    /**
     * 获取所有基础信息数据
     * code 0 成功
     * page 分页 limit 条数
     * @return
     */
    @RequestMapping("basePhone/list")
    public Object listBase(@RequestParam(name = "page", defaultValue = "1") Integer page,
                           @RequestParam("limit") Integer limit) {
        JSONObject jsonObject = new JSONObject();
        // 全部数据
        List<PhoneBase> phoneBases = phoneBaseService.allListPhone();
        // 三元表达式 来解决页数问题
        Integer start = (page-1)*limit < 0 ? 0 : (page-1)*limit;
        Integer end = page*limit > phoneBases.size() ? phoneBases.size() : page*limit;
        // 前台需要的数据 code状态码  count总页数
        jsonObject.put("data", phoneBases.subList(start, end));
        jsonObject.put("code", 0);
        jsonObject.put("count", phoneBases.size());
        return jsonObject;
    }


    /**
     * 添加基础数据
     * @param base
     * @return
     */
    @PostMapping("basePhone/add")
    @LoginRequired
    public Object setBase(@RequestBody PhoneBase base) {
        JSONObject jsonObject = new JSONObject();
        Integer msg = 0;
        jsonObject.put("base", base);
        if (base.getBaseId() != null) {
            msg = phoneBaseService.addPhonoBase(base);
        }
        jsonObject.put("msg", msg);
        return jsonObject;
    }

    /**
     * 修改基础数据
     * @param base
     * @return
     */
    @PutMapping("basePhone/alter")
    @LoginRequired
    public Object alterBase(@RequestBody PhoneBase base) {
        JSONObject jsonObject = new JSONObject();
        Integer msg = 0;
        jsonObject.put("base", base);
        if (base.getBaseId() != null) {
            msg = phoneBaseService.updatePhoneBase(base);
        }
        jsonObject.put("msg", msg);
        return jsonObject;
    }

    /**
     * 删除基本数据 根据id
     * @return
     */
    @DeleteMapping("basePhone/delete/{id}")
    @LoginRequired
    public Object deleteBase(@PathVariable int id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", phoneBaseService.deletePhoneBaseByid(id));
        return jsonObject;
    }

    /**
     * 多个基本数据的删除
     * @param baseLists
     * @return
     */
    @PostMapping("basePhone/delete/list")
    @LoginRequired
    public Object deleteBaseList(@RequestBody List baseLists) {
        JSONObject jsonObject = new JSONObject();
        baseLists.forEach(n -> jsonObject.put(String.valueOf(n), phoneBaseService.deletePhoneBaseByid((Integer) n)));
        return jsonObject;
    }
}
